home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / C / CRACKLIB.ZIP / CRACKLIB.TAR / cracklib25_small / cracklib / fascist.c next >
Encoding:
C/C++ Source or Header  |  1993-07-09  |  8.3 KB  |  457 lines

  1. /*
  2.  * This program is copyright Alec Muffett 1993. The author disclaims all 
  3.  * responsibility or liability with respect to it's usage or its effect 
  4.  * upon hardware or computer systems, and maintains copyright as set out 
  5.  * in the "LICENCE" document which accompanies distributions of Crack v4.0 
  6.  * and upwards.
  7.  */
  8.  
  9. static char vers_id[] = "fascist.c : v2.3p2 Alec Muffett 18 May 1993";
  10.  
  11. #include "packer.h"
  12. #include <ctype.h>
  13. #include <sys/types.h>
  14. #include <pwd.h>
  15.  
  16. #define ISSKIP(x) (isspace(x) || ispunct(x))
  17.  
  18. #define MINDIFF    5
  19. #define MINLEN 6
  20. #define MAXSTEP 4
  21.  
  22. static char *r_destructors[] = {
  23.     ":",            /* noop - must do this to test raw word. */
  24.  
  25.     "[",            /* trimming leading/trailing junk */
  26.     "]",
  27.     "[[",
  28.     "]]",
  29.     "[[[",
  30.     "]]]",
  31.  
  32.     "/?p@?p",            /* purging out punctuation/symbols/junk */
  33.     "/?s@?s",
  34.     "/?X@?X",
  35.  
  36.     "/$s$s",            /* transmogrifying "$" back into "s", etc... */
  37.     "/4s4a",
  38.     "/2s2a",
  39.     "/3s3e",
  40.     "/0s0o",
  41.     "/1s1i",
  42.     "/4s4h",
  43.     "/1s1l",
  44.     "/$s$s/1s1l",
  45.     "/2s2a/3s3e",
  46.     "/0s0o/4s4h",
  47.     "/1s1l/4s4a",
  48.     "/2s2a/4s4h",
  49.     "/$s$s/3s3e",
  50.     "/$s$s/0s0o",
  51.     "/1s1i/4s4a",
  52.     "/$s$s/2s2a",
  53.     "/$s$s/4s4a",
  54.     "/0s0o/1s1l",
  55.     "/3s3e/4s4h",
  56.     "/1s1l/4s4h",
  57.     "/$s$s/4s4h",
  58.     "/1s1i/4s4h",
  59.     "/2s2a/4s4a",
  60.     "/$s$s/1s1i",
  61.     "/0s0o/1s1i",
  62.     "/1s1i/2s2a",
  63.     "/1s1i/3s3e",
  64.     "/0s0o/4s4a",
  65.     "/1s1l/3s3e",
  66.     "/3s3e/4s4a",
  67.     "/0s0o/2s2a",
  68.     "/0s0o/3s3e",
  69.     "/1s1l/2s2a",
  70.     "/0s0o/1s1l/2s2a",
  71.     "/0s0o/1s1i/4s4h",
  72.     "/1s1l/2s2a/4s4a",
  73.     "/2s2a/3s3e/4s4a",
  74.     "/0s0o/1s1i/2s2a",
  75.     "/1s1i/2s2a/3s3e",
  76.     "/0s0o/1s1l/4s4a",
  77.     "/1s1l/2s2a/4s4h",
  78.     "/$s$s/0s0o/1s1l",
  79.     "/$s$s/0s0o/2s2a",
  80.     "/0s0o/1s1i/3s3e",
  81.     "/$s$s/0s0o/3s3e",
  82.     "/1s1i/2s2a/4s4a",
  83.     "/$s$s/0s0o/4s4a",
  84.     "/$s$s/0s0o/4s4h",
  85.     "/0s0o/1s1l/4s4h",
  86.     "/1s1i/2s2a/4s4h",
  87.     "/0s0o/1s1l/3s3e",
  88.     "/$s$s/0s0o/1s1i",
  89.     "/0s0o/1s1i/4s4a",
  90.     "/2s2a/3s3e/4s4h",
  91.     "/1s1l/2s2a/3s3e",
  92.     "/$s$s/0s0o/1s1l/3s3e",
  93.     "/0s0o/1s1l/2s2a/4s4a",
  94.     "/0s0o/1s1i/2s2a/3s3e",
  95.     "/$s$s/0s0o/1s1i/4s4a",
  96.     "/$s$s/0s0o/1s1i/3s3e",
  97.     "/$s$s/0s0o/1s1l/4s4a",
  98.     "/0s0o/1s1l/2s2a/3s3e",
  99.     "/$s$s/0s0o/1s1i/2s2a",
  100.     "/1s1l/2s2a/3s3e/4s4a",
  101.     "/$s$s/0s0o/1s1l/2s2a",
  102.     "/$s$s/0s0o/1s1i/4s4h",
  103.     "/1s1i/2s2a/3s3e/4s4a",
  104.     "/0s0o/1s1i/2s2a/4s4a",
  105.     "/0s0o/1s1l/2s2a/4s4h",
  106.     "/1s1i/2s2a/3s3e/4s4h",
  107.     "/1s1l/2s2a/3s3e/4s4h",
  108.     "/0s0o/1s1i/2s2a/4s4h",
  109.     "/$s$s/0s0o/1s1l/4s4h",
  110.     "/$s$s/0s0o/1s1i/2s2a/4s4a",
  111.     "/$s$s/0s0o/1s1l/2s2a/4s4h",
  112.     "/$s$s/0s0o/1s1i/2s2a/4s4h",
  113.     "/0s0o/1s1l/2s2a/3s3e/4s4a",
  114.     "/$s$s/0s0o/1s1l/2s2a/4s4a",
  115.     "/0s0o/1s1i/2s2a/3s3e/4s4a",
  116.     "/0s0o/1s1i/2s2a/3s3e/4s4h",
  117.     "/$s$s/0s0o/1s1l/2s2a/3s3e",
  118.     "/0s0o/1s1l/2s2a/3s3e/4s4h",
  119.     "/$s$s/0s0o/1s1i/2s2a/3s3e",
  120.     "/$s$s/0s0o/1s1l/2s2a/3s3e/4s4h",
  121.     "/$s$s/0s0o/1s1l/2s2a/3s3e/4s4a",
  122.     "/$s$s/0s0o/1s1i/2s2a/3s3e/4s4a",
  123.     "/$s$s/0s0o/1s1i/2s2a/3s3e/4s4h",
  124.     (char *) 0
  125. };
  126.  
  127. static char *r_constructors[] = {
  128.     ":",
  129.     "r",
  130.     "d",
  131.     "f",
  132.     "dr",
  133.     "fr",
  134.     "rf",
  135.     (char *) 0
  136. };
  137.  
  138. int
  139. GTry(rawtext, password)
  140.     register char *rawtext;
  141.     register char *password;
  142. {
  143.     register int i;
  144.     register char *mp;
  145.     int len;
  146.  
  147.     /* use destructors to turn password into rawtext */
  148.     /* note use of Reverse() to save duplicating all rules */
  149.  
  150.     len = strlen(password);
  151.  
  152.     for (i = 0; r_destructors[i]; i++)
  153.     {
  154.     if (!(mp = Mangle(password, r_destructors[i])))
  155.     {
  156.         continue;
  157.     }
  158.  
  159. #ifdef DEBUG
  160.     printf("d1->\t%s vs %s\n", mp, rawtext);
  161. #endif
  162.     if (!strncmp(mp, rawtext, len))
  163.     {
  164.         return (1);
  165.     }
  166. #ifdef DEBUG
  167.     printf("d2->\tr(%s) vs %s\n", mp, rawtext);
  168. #endif
  169.     if (!strncmp(Reverse(mp), rawtext, len))
  170.     {
  171.         return (1);
  172.     }
  173.     }
  174.  
  175.     /* use constructors to turn rawtext into password */
  176.  
  177.     for (i = 0; r_constructors[i]; i++)
  178.     {
  179.     if (!(mp = Mangle(rawtext, r_constructors[i])))
  180.     {
  181.         continue;
  182.     }
  183. #ifdef DEBUG
  184.     printf("c->\t%s vs %s\n", mp, password);
  185. #endif
  186.     if (!strncmp(mp, password, len))
  187.     {
  188.         return (1);
  189.     }
  190.     }
  191.  
  192.     return (0);
  193. }
  194.  
  195. char *
  196. FascistGecos(password, uid)
  197.     char *password;
  198.     int uid;
  199. {
  200.     int i;
  201.     int j;
  202.     int wc;
  203.     struct passwd *pwp;
  204.     char gbuffer[STRINGSIZE];
  205.     char *uwords[STRINGSIZE];
  206.     char buffer[STRINGSIZE];
  207.     register char *ptr = 
  208.         "\101\154\145\143\127\141\163\110\145\162\145";
  209.  
  210.     if (!(pwp = getpwuid(uid)))
  211.     {
  212.     return ("you are not registered in the password file");
  213.     }
  214.  
  215.     if (GTry(pwp->pw_name, password))
  216.     {
  217.     return ("it is based on your username");
  218.     }
  219.  
  220.     strcpy(gbuffer, Lowercase(pwp->pw_gecos));
  221.  
  222.     wc = 0;
  223.     ptr = gbuffer;
  224.  
  225.     while (*ptr)
  226.     {
  227.     while (*ptr && ISSKIP(*ptr))
  228.     {
  229.         ptr++;
  230.     }
  231.  
  232.     if (ptr != gbuffer)
  233.     {
  234.         ptr[-1] = '\0';
  235.     }
  236.  
  237.     uwords[wc++] = ptr;
  238.  
  239.     if (wc == STRINGSIZE)
  240.     {
  241.         uwords[--wc] = (char *) 0;    /* to hell with it */
  242.         break;
  243.     } else
  244.     {
  245.         uwords[wc] = (char *) 0;
  246.     }
  247.  
  248.     while (*ptr && !ISSKIP(*ptr))
  249.     {
  250.         ptr++;
  251.     }
  252.  
  253.     if (*ptr)
  254.     {
  255.         *(ptr++) = '\0';
  256.     }
  257.     }
  258. #ifdef DEBUG
  259.     for (i = 0; uwords[i]; i++)
  260.     {
  261.         printf("u:\t%s\n", uwords[i]);
  262.     }
  263. #endif
  264.     for (i = 0; uwords[i]; i++)
  265.     {
  266.     if (GTry(uwords[i], password))
  267.     {
  268.         return ("it is based upon your password entry");
  269.     }
  270.     }
  271.  
  272.     for (j = 1; uwords[j]; j++)
  273.     {
  274.     for (i = 0; i < j; i++)
  275.     {
  276.         strcpy(buffer, uwords[i]);
  277.         strcat(buffer, uwords[j]);
  278.         if (GTry(buffer, password))
  279.         {
  280.         return ("it is derived from your password entry");
  281.         }
  282.  
  283.         strcpy(buffer, uwords[j]);
  284.         strcat(buffer, uwords[i]);
  285.         if (GTry(buffer, password))
  286.         {
  287.         return ("it's derived from your password entry");
  288.         }
  289.  
  290.         buffer[0] = uwords[i][0];
  291.         buffer[1] = '\0';
  292.         strcat(buffer, uwords[j]);
  293.         if (GTry(buffer, password))
  294.         {
  295.         return ("it is derivable from your password entry");
  296.         }
  297.  
  298.         buffer[0] = uwords[j][0];
  299.         buffer[1] = '\0';
  300.         strcat(buffer, uwords[i]);
  301.         if (GTry(buffer, password))
  302.         {
  303.         return ("it's derivable from your password entry");
  304.         }
  305.     }
  306.     }
  307.  
  308.     return ((char *) 0);
  309. }
  310.  
  311. char *
  312. FascistLook(pwp, instring)
  313.     PWDICT *pwp;
  314.     char *instring;
  315. {
  316.     register int i;
  317.     register char *ptr;
  318.     char junk[STRINGSIZE];
  319.     register char *jptr;
  320.     char rpassword[STRINGSIZE];
  321.     register char *password;
  322.     int32 notfound;
  323.  
  324.     notfound = PW_WORDS(pwp);
  325.     password = rpassword;
  326.  
  327.     strncpy(password, instring, STRINGSIZE);
  328.  
  329.     password[STRINGSIZE - 1] = '\0';
  330.  
  331.     if (strlen(password) < 4)
  332.     {
  333.     return ("it's WAY too short");
  334.     }
  335.  
  336.     if (strlen(password) < MINLEN)
  337.     {
  338.     return ("it is too short");
  339.     }
  340.  
  341.     jptr = junk;
  342.     *jptr = '\0';
  343.  
  344.     for (i = 0; i < STRINGSIZE && password[i]; i++)
  345.     {
  346.     if (!strchr(junk, password[i]))
  347.     {
  348.         *(jptr++) = password[i];
  349.         *jptr = '\0';
  350.     }
  351.     }
  352.  
  353.     if (strlen(junk) < MINDIFF)
  354.     {
  355.     return ("it does not contain enough DIFFERENT characters");
  356.     }
  357.  
  358.     strcpy(password, Lowercase(password));
  359.  
  360.     Trim(password);
  361.  
  362.     while (*password && isspace(*password))
  363.     {
  364.     password++;
  365.     }
  366.  
  367.     if (!*password)
  368.     {
  369.     return ("it is all whitespace");
  370.     }
  371.  
  372.     i = 0;
  373.     ptr = password;
  374.     while (ptr[0] && ptr[1])
  375.     {
  376.     if ((ptr[1] == (ptr[0] + 1)) || (ptr[1] == (ptr[0] - 1)))
  377.     {
  378.         i++;
  379.     }
  380.     ptr++;
  381.     }
  382.  
  383.     if (i > MAXSTEP)
  384.     {
  385.     return ("it is too simplistic/systematic");
  386.     }
  387.  
  388.     if (PMatch("aadddddda", password))    /* smirk */
  389.     {
  390.     return ("it looks like a National Insurance number.");
  391.     }
  392.  
  393.     if (ptr = FascistGecos(password, getuid()))
  394.     {
  395.     return (ptr);
  396.     }
  397.  
  398.     for (i = 0; r_destructors[i]; i++)
  399.     {
  400.     char *a;
  401.     if (!(a = Mangle(password, r_destructors[i])))
  402.     {
  403.         continue;
  404.     }
  405.     if (FindPW(pwp, a) != notfound)
  406.     {
  407.         return ("it is based on a dictionary word");
  408.     }
  409.     }
  410.  
  411.     strcpy(password, Reverse(password));
  412.  
  413.     for (i = 0; r_destructors[i]; i++)
  414.     {
  415.     char *a;
  416.  
  417.     if (!(a = Mangle(password, r_destructors[i])))
  418.     {
  419.         continue;
  420.     }
  421.  
  422.     if (FindPW(pwp, a) != notfound)
  423.     {
  424.         return ("it is based on a (reversed) dictionary word");
  425.     }
  426.     }
  427.  
  428.     return ((char *) 0);
  429. }
  430.  
  431. char *
  432. FascistCheck(password, path)
  433.     char *password;
  434.     char *path;
  435. {
  436.     static PWDICT *pwp;
  437.     static char lastpath[STRINGSIZE];
  438.  
  439.     if (pwp && strncmp(lastpath, path, STRINGSIZE))
  440.     {
  441.         PWClose(pwp);
  442.         pwp = (PWDICT *)0;
  443.     }
  444.  
  445.     if (!pwp)
  446.     {
  447.     if (!(pwp = PWOpen(path, "r")))
  448.     {
  449.         perror("PWOpen");
  450.         exit(-1);
  451.     }
  452.     strncpy(lastpath, path, STRINGSIZE);
  453.     }
  454.  
  455.     return (FascistLook(pwp, password));
  456. }
  457.